package com.huqs.smoothphotoview.library.fragment;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.huqs.smoothphotoview.library.progress.LoaderProgressTarget;
import com.huqs.smoothphotoview.library.ui.PhotoActivity;
import com.huqs.smoothphotoview.library.view.RoundNumProgressView;
import com.huqs.smoothphotoview.library.view.SmoothImageView;
import com.huqs.smoothphotoviewer.library.R;
import uk.co.senab.photoview.PhotoViewAttacher;
public class PhotoFragment extends Fragment{
    private SmoothImageView ivPhoto;
    private RoundNumProgressView progressBar;
    private Rect startBounds;
    private Activity activity;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_photo, null);
        activity = getActivity();
        ivPhoto = (SmoothImageView) rootView.findViewById(R.id.iv_photo);
        ivPhoto.setMinimumScale(1f);
        ivPhoto.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
            @Override
            public void onViewTap(View view, float x, float y) {
                ((PhotoActivity) activity).transformOut();
            }
        });
        progressBar = (RoundNumProgressView) rootView.findViewById(R.id.num_Progress_view);
        progressBar.setRotateOrientation(RoundNumProgressView.CLOCKWISE);
        Bundle args = getArguments();
        if (args != null && args.containsKey("img")) {
           final  String imgUrl = args.getString("img");
            startBounds = args.getParcelable("startBounds");
            ivPhoto.setTransformEnabled(true);
            LoaderProgressTarget<GlideDrawable> progressTarget = new LoaderProgressTarget<>(new GlideDrawableImageViewTarget(ivPhoto), progressBar);
            progressTarget.setModel(imgUrl);
            Glide.with(PhotoFragment.this).load(imgUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(progressTarget);
        }
        return rootView;
    }
    public void transformIn(SmoothImageView.onTransformListener listener) {
        if (ivPhoto!=null)
        ivPhoto.transformIn(startBounds, listener);
    }

    public void transformOut(SmoothImageView.onTransformListener listener) {
        if (ivPhoto!=null)
        ivPhoto.transformOut(startBounds, listener);
    }


}
